#include<iostream>
#include<string>
#include<algorithm>

using namespace std;
int ans = 0x3f3f3f3f;
string a, b;

void dfs(string t, int pos, int step) {
    if (t[pos] < a[pos]) {
        ans = min(ans, step);
        return;
    }
    if (pos + 1 < a.size() && pos + 1 == b.size() && t[pos] == a[pos]) {
        ans = min(ans, step);
        return;
    }
    if (pos + 1 == a.size())return;
    string str = t;
    for (int j = 0; j < pos; j++)
        str[j] = t[j];
    for (int i = pos + 1; i < t.size(); i++) {
        if (t[i] <= a[pos]) {
            str[pos] = t[i];
            for (int j = pos; j < i; j++)
                str[j + 1] = t[j];
            for (int j = i; j < t.size(); j++)
                str[j] = t[j];
            dfs(str, pos + 1, step + i - pos);
        }
    }
}

int main() {
    cin >> b >> a;
    dfs(b, 0, 0);
    if (ans == 0x3f3f3f3f)cout << -1 << "\n";
    else cout << ans << "\n";
    return 0;